package vn.gambaro.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class DemoImageViewAnimActivity extends Activity {
    /** Called when the activity is first created. */
	 private static final String LOGID = "CHECKTHISOUT";
	    public Integer[] list = {R.drawable.b_1,R.drawable.b_2, R.drawable.b_3, R.drawable.b_4, R.drawable.b_5 };
	    private static final int SWIPE_MIN_DISTANCE = 120;
	    private static final int SWIPE_MAX_OFF_PATH = 250;
	    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	    private GestureDetector gestureDetector;
	    View.OnTouchListener gestureListener;
	    public int i = 0;

	    String message = "Initial Message"; 
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        setContentView(R.layout.main);

	        ImageView imageview = (ImageView)findViewById(R.id.imageView1);
	        imageview.setImageResource(list[0]);

	        gestureDetector = new GestureDetector(new MyGestureDetector());
	        
	    }

	    public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener {
	        @Override
	        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	            try {
	                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
	                    return false;
	                // right to left swipe
	                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
	                    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
//	                    if(list.length>i){
//	                        i++;
	                    if(i>0){
		                   i--;
	                    imageview.setImageResource(list[i]);
	                    }
	                    Log.v(LOGID,"right to left swipe detected");
	                    System.out.println("right to left swipe detected");


	                } // left to right swipe 
	                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
	                    ImageView imageview = (ImageView)findViewById(R.id.imageView1);
	                    if(list.length>i){
	                    	i++;
//	                    if(i>0){
//	                        i--;
	                    imageview.setImageResource(list[i]);
	                    }
	                    Log.v(LOGID,"left to right swipe detected");      
	                    System.out.println("left to right swipe detected");


	                }
	            } catch (Exception e) {
	                // nothing
	            }
	            return false;
	        }
	    }

	    // This doesn't work
	    @Override
	    public boolean onTouchEvent(MotionEvent event) {
	        if (gestureDetector.onTouchEvent(event)){
	            Log.v(LOGID,"screen touched");
	            System.out.println("screen touched");
	            return true;
	        }
	        else{
	            return false;
	        }
	    }
}
